//
// Created by bbnf on 09.06.2024.
//

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <cstddef>
#include <filesystem>
#include <sstream>
#include <chrono>
#include <ctime>
#include <iostream>
#include <storage/file_storage/file_parser.h>
#include <format>

namespace {
    class TxtFileParseTest : public ::testing::Test {
    private:
        std::filesystem::path m_path;
    protected:
        TodoApp::Storage::FileStorage::FileParser *m_parser;

        TxtFileParseTest() {
            m_path = std::filesystem::path(std::format("{}/todos.txt", BINARY_DIR));
            m_parser = new TodoApp::Storage::FileStorage::TxtFileParser(m_path);
        }

        ~TxtFileParseTest() override = default;

        void SetUp() override {
            std::stringstream ss;
            auto t = std::chrono::system_clock::now();
            ss << "0||Todo||description||" << 0 << "||" << t.time_since_epoch().count() << "\n";
            ss << "1||Todo1||Description1||" << 0 << "||"
               << t.time_since_epoch().count() - (static_cast<int64_t >(60 * 60 * 24))
               << "\n";

            std::fstream file;
            file.open(m_path, std::ios::out);
            if (file.is_open()) {
                file << ss.str();
            }
            file.close();
        }

        void TearDown() override {
            delete m_parser;
            std::filesystem::remove(m_path);
        }
    };

    TEST_F(TxtFileParseTest, TestParse) {
        auto todos = m_parser->parse();
        EXPECT_EQ(todos.size(), 2);

        EXPECT_FALSE(todos[0].done);
        EXPECT_TRUE(todos[1].done);
    }
}
